package com.example.demo.service;

import com.example.demo.enums.BookStatus;
import com.example.demo.mapper.BookInfoMapper;
import com.example.demo.model.BookInfo;
import com.example.demo.model.PageRequest;
import com.example.demo.model.PageResult;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {
    @Autowired
    private BookInfoMapper bookInfoMapper;

    public PageResult getPageResult(PageRequest pageRequest) {
        PageResult<BookInfo> pageResult = new PageResult<>();

        List<BookInfo> infos = bookInfoMapper.getBookInfos(pageRequest);
        for(BookInfo info : infos) {
            info.setStatusCN(BookStatus.getDescByCode(info.getStatus()).getDesc());
        }

        pageResult.setInfos(infos);
        pageResult.setCount(bookInfoMapper.getCount());
        pageResult.setPageRequest(pageRequest);
        return pageResult;
    }

    public Boolean addBook(BookInfo bookInfo) {
        Integer ret = bookInfoMapper.addBookInfo(bookInfo);
        if(ret <= 0) return false;
        return true;
    }

    public BookInfo getBookById(Integer id) {
        return bookInfoMapper.getBookById(id);
    }

    public Boolean update(BookInfo bookInfo) {
        Integer ret = bookInfoMapper.update(bookInfo);
        if(ret <= 0) return false;
        return true;
    }

    public Boolean deleteById(Integer id) {
        Integer ret = bookInfoMapper.deleteById(id);
        if(ret <= 0) return false;
        return true;
    }

    public Boolean deleteByBatch(List<Integer> ids) {
        Integer ret = bookInfoMapper.deleteByBatch(ids);
        if(ret != ids.size()) return false;
        return true;
    }
}
